package cz.acies.genea.data.persist;

import java.util.LinkedHashMap;
import java.util.Map;

import cz.acies.data.persist.ModelObject;

public class GedFamily extends ModelObject implements Cloneable
{
	//private String id;
	private String projectId;
	private GedPerson man;
	private GedPerson woman;
	private java.util.Date weddingDate;
	private String weddingPlace;
	
	private GedEventFact eventFact;

	private Map<String, GedPerson> children = null;
	
	public GedFamily() {
		this(null, null, null);
	}

	public GedFamily(String id) {
		this(id, null, null);
	}

	public GedFamily(String id, Map<String, Object> data) {
		this.id = id;
		String manId = (String) data.get("manId");
		if (manId != null && manId.equalsIgnoreCase("NULL")) manId = "";
		this.man = new GedPerson(manId);
		String womanId = (String) data.get("womanId");
		if (womanId != null && womanId.equalsIgnoreCase("NULL")) womanId = "";
		this.woman = new GedPerson(womanId);
		this.children = new LinkedHashMap<String, GedPerson>();
	}
	
	public GedFamily(String id, String manId, String womenId) {
		this.id = id;
		this.man = new GedPerson(manId);
		this.woman = new GedPerson(womenId);
		this.children = new LinkedHashMap<String, GedPerson>();
	}

	public GedFamily(GedPerson man, GedPerson woman) {
		this.man = man;
		this.woman = woman;
		this.children = new LinkedHashMap<String, GedPerson>();
	}

	// -----  ----------------------------------------------------------------
	/*
	public String getId() {
		return id;
	}

	public boolean hasId() {
		return (id != null && id.trim().length() > 0);
	}
	
	public void setId(String id) {
		this.id = id;
	}
	*/
	public String getProjectId() {
		return projectId;
	}

	public boolean hasProjectId() {
		return (projectId != null &&  projectId.startsWith("GE.P."));
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}
	
	public GedPerson getMan() {
		return man;
	}

	public void setMan(GedPerson man) {
		this.man = man;
	}

	public boolean hasMan() {
		return (man != null);
	}

	public GedPerson getWoman() {
		return woman;
	}

	public void setWoman(GedPerson woman) {
		this.woman = woman;
	}
	
	public boolean hasWoman() {
		return (woman != null);
	}
	
	public java.util.Date getWeddingDate() {
		return weddingDate;
	}
	
	public boolean hasWeddingDate() {
		return (weddingDate != null);
	}

	public void setWeddingDate(java.util.Date weddingDate) {
		this.weddingDate = weddingDate;
	}

	public boolean hasWeddingPlace() {
		return (weddingPlace != null);
	}

	public String getWeddingPlace() {
		return weddingPlace;
	}

	public void setWeddingPlace(String weddingPlace) {
		this.weddingPlace = weddingPlace;
	}

	
	public GedEventFact getEventFact() {
		return eventFact;
	}

	public boolean hasEventFact() {
		return eventFact != null;
	}

	public void setEventFact(GedEventFact eventFact) {
		this.eventFact = eventFact;
	}

	public void setEventFact(String id) {
		if (this.eventFact == null) {
			this.eventFact = new GedEventFact(id);
		} else {
			this.eventFact.setId(id);
		}
	}

	public Map<String, GedPerson> getChildren() {
		return children;
	}

	public void setChildren(Map<String, GedPerson> childern) {
		this.children = childern;
	}

	public void addChild(GedPerson child) {
		if (children == null) children = new LinkedHashMap<String, GedPerson>();
		if (child == null || !child.hasId()) return;
		children.put(child.getId(), child);
	}

	// -----  -----------------------------------------------------------------

	@Override
	public String toString() {
		return "GedFamily [\n" +
			"\t Man:   " + this.man + "\n" +
			"\t Woman: " + this.woman + "\n" +
			"\t Childern: " + this.children + "\n" +
		"]"; 
	}

	public Object clone() throws CloneNotSupportedException {
		Object clone = super.clone();
		assert clone instanceof GedFamily;
		
		((GedFamily) clone).setId(this.id);
		((GedFamily) clone).setProjectId(this.projectId);
		((GedFamily) clone).setWeddingDate(this.weddingDate);
		((GedFamily) clone).setWeddingPlace(this.weddingPlace);
		
		
		((GedFamily) clone).setMan((man != null) ? (GedPerson) man.clone()  : null);
		((GedFamily) clone).setWoman((woman != null) ? (GedPerson) woman.clone()  : null);
		//((GedFamily) clone).setEventFact((eventFact != null) ? (GedEventFact) eventFact.clone()  : null);
		((GedFamily) clone).setChildren((children != null) ? 
			(LinkedHashMap<String, GedPerson>)((LinkedHashMap<String, GedPerson>)children).clone() : null);

		return clone;
	}
}
